package com.bruce.tool.web.filter.core;

import com.bruce.tool.web.filter.util.RequestUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class WrapperFilter implements Filter {
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (RequestUtils.isJson(request)) {
            JsonRequestWrapper myRequestWrapper = new JsonRequestWrapper((HttpServletRequest) servletRequest);
            filterChain.doFilter( myRequestWrapper, servletResponse );
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
